package com.photoeditor.demo.model.share;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.app.ItemClickSupport;
import com.photoeditor.demo.db.DataManager;
import com.photoeditor.demo.model.image.util.ImageHelper;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.model.bean.IItemBean;
import com.photoeditor.demo.ui.adapter.share.ShareImageAdapter;
import com.photoeditor.demo.ui.widget.share.ShareImageItem;
import com.photoeditor.demo.ui.widget.share.ShareImageLittleItem;
import com.photoeditor.demo.ui.adapter.share.ShareImageLittleListAdapter;
import com.photoeditor.demo.statistics.NotifyStoreUpdateStatisticUtil;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.util.BitmapDecoder;
import com.photoeditor.demo.util.FontUtil;
import com.photoeditor.demo.util.MediaTypeUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static android.support.design.widget.BottomSheetBehavior.STATE_COLLAPSED;

public class ShareImageDialogHelp {

    private Activity mContext;

    private final int DEFAULT_SIZE;

    private AlertDialog mDialog;

    private GridView mShareGridView;
    private ShareImageLittleListAdapter mShareImageAdapter;

    private ImageView mShareImage;

    private BitmapBean mBitmapBean;

    private ProgressDialog mShareDialog;

    private View mEndLine;

    public ShareImageDialogHelp(Activity activity){
        mContext = activity;
        DEFAULT_SIZE = activity.getResources().getDimensionPixelSize(R.dimen.share_imageview_max_width);
    }

    public void destory(){
    }

    public void showShareDialog(Bitmap bitmap, Uri u){
        mBitmapBean = ImageHelper.getInterBitmapBeanFormUri(mContext, u);
        if(mBitmapBean == null){
            return;
        }
        boolean isImage = MediaTypeUtil.isImage(mBitmapBean.mType);
        boolean isGif = MediaTypeUtil.isGif(mBitmapBean.mType);
        if(mDialog == null){
            mDialog = new AlertDialog.Builder(mContext, R.style.AlertDialogThemeNoBackground).create();
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.show();
            FontUtil.setAlertDialogCustomFont(mDialog);
            Window window = mDialog.getWindow();
            View view = mContext.getLayoutInflater().inflate(R.layout.layout_share_image_dialog, null, false);
            window.setContentView(view);
            WindowManager.LayoutParams lp = window.getAttributes();
            boolean landscape = false;
            DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
            int SCREEN_WIDTH = dm.widthPixels;
            int SCREEN_HEIGHT = dm.heightPixels;
            if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                landscape = true;
            }
            if(landscape){
                lp.width = (int) (SCREEN_HEIGHT * 0.9f);
            } else {
                lp.width = (int) (SCREEN_WIDTH * 0.9f);
            }
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.windowAnimations = R.style.myCustomDialog;
            window.setAttributes(lp);
            mShareGridView = (GridView) view.findViewById(R.id.share_gridview);
            mShareImage = (ImageView) view.findViewById(R.id.share_image);
            mEndLine = view.findViewById(R.id.end_line);
            mShareImageAdapter = new ShareImageLittleListAdapter(mContext, ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif));
            mShareGridView.setAdapter(mShareImageAdapter);
            bindListener();
        } else{
            mShareImageAdapter.setData(ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif));
            mShareGridView.setAdapter(mShareImageAdapter);
            mDialog.show();
        }

        if(bitmap == null){
            setBitmapAndResize(null);
            BitmapDecoder.getBitmap(mBitmapBean.mPath, mBitmapBean.mDegree, DEFAULT_SIZE, new ILoadBitmap() {
                @Override
                public void onLoadOver(Bitmap bitmap) {
                    if(mDialog != null && mDialog.isShowing()){
                        setBitmapAndResize(bitmap);
                    }
                }
            });
        } else{
            setBitmapAndResize(bitmap);
        }
    }

    private BottomSheetDialog mBottomDialog;
    private RecyclerView mShareRecyclerView;
    private ShareImageAdapter mRecyclerAdapter;
    private String mMainButtonEntrance;

    public void setMainButtonEntrance(String mainButtonEntrance) {
        mMainButtonEntrance = mainButtonEntrance;
    }

    public void showShareDialogBottom(BitmapBean bean){
        mBitmapBean = bean;
        boolean isImage = MediaTypeUtil.isImage(bean.mType);
        boolean isGif = MediaTypeUtil.isGif(bean.mType);
        if(mDialog == null){
            mBottomDialog = new BottomSheetDialog(mContext);
            View contentView = mContext.getLayoutInflater().inflate(R.layout.dialog_share_img_bottom, null, false);
            mShareRecyclerView = (RecyclerView) contentView.findViewById(R.id.rv_share);
            mRecyclerAdapter = new ShareImageAdapter();

            List<IItemBean> datas = new ArrayList<>();
            List<ShareImageItem.ShareImageItemData> allShareTools = ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif);
            if (allShareTools != null) {
                for (ShareImageItem.ShareImageItemData data : allShareTools) {
                    ShareImageAdapter.ShareImageItemDataBean shareImageItemDataBean = new ShareImageAdapter.ShareImageItemDataBean();
                    shareImageItemDataBean.set(data);
                    datas.add(shareImageItemDataBean);
                }
            }
            mRecyclerAdapter.setData(datas);
            mShareRecyclerView.setAdapter(mRecyclerAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4);
            mShareRecyclerView.setLayoutManager(gridLayoutManager);
            bindListenerBottom();
            mBottomDialog.setContentView(contentView);
            View parent = mBottomDialog.getWindow().findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
            if(isImage) {
                behavior.setPeekHeight(DeviceUtils.dip2px(mContext, 193));
            }else{
                behavior.setPeekHeight(DeviceUtils.dip2px(mContext, 120));
            }
            mBottomDialog.show();
        }else{

            List<IItemBean> datas = new ArrayList<>();
            List<ShareImageItem.ShareImageItemData> allShareTools = ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif);
            if (allShareTools != null) {
                for (ShareImageItem.ShareImageItemData data : allShareTools) {
                    ShareImageAdapter.ShareImageItemDataBean shareImageItemDataBean = new ShareImageAdapter.ShareImageItemDataBean();
                    shareImageItemDataBean.set(data);
                    datas.add(shareImageItemDataBean);
                }
            }
            mRecyclerAdapter.setData(datas);
            mRecyclerAdapter.notifyDataSetChanged();
            mBottomDialog.show();
        }
    }

    public void showShareDialog(Bitmap bitmap, BitmapBean bean){
        mBitmapBean = bean;
        boolean isImage = MediaTypeUtil.isImage(bean.mType);
        boolean isGif = MediaTypeUtil.isGif(bean.mType);
        if(mDialog == null){
            mDialog = new AlertDialog.Builder(mContext, R.style.AlertDialogThemeNoBackground).create();
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
            Window window = mDialog.getWindow();
            View view = mContext.getLayoutInflater().inflate(R.layout.layout_share_image_dialog, null, false);
            window.setContentView(view);
            WindowManager.LayoutParams lp = window.getAttributes();
            boolean landscape = false;
            DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
            int SCREEN_WIDTH = dm.widthPixels;
            int SCREEN_HEIGHT = dm.heightPixels;
            if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                landscape = true;
            }
            if(landscape){
                lp.width = (int) (SCREEN_HEIGHT * 0.9f);
            } else {
                lp.width = (int) (SCREEN_WIDTH * 0.9f);
            }
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.windowAnimations = R.style.myCustomDialog;
            window.setAttributes(lp);
            mShareGridView = (GridView) view.findViewById(R.id.share_gridview);
            mShareImage = (ImageView) view.findViewById(R.id.share_image);
            mEndLine = view.findViewById(R.id.end_line);
            FontUtil.setCustomFont((TextView) view.findViewById(R.id.share_title));
            mShareImageAdapter = new ShareImageLittleListAdapter(mContext, ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif));
            mShareGridView.setAdapter(mShareImageAdapter);
            bindListener();
        } else{
            mShareImageAdapter.setData(ShareImageTools.getTop3ShareTools(mContext, isImage ? 1 : 2, true, !isGif));
            mShareImageAdapter.notifyDataSetChanged();
            mDialog.show();
        }

        if(bitmap == null){
            setBitmapAndResize(null);
            BitmapDecoder.getBitmap(bean.mPath, bean.mDegree, DEFAULT_SIZE, new ILoadBitmap() {
                @Override
                public void onLoadOver(Bitmap bitmap) {
                    if(mDialog != null && mDialog.isShowing()){
                        setBitmapAndResize(bitmap);
                    }
                }
            });
        } else{
            setBitmapAndResize(bitmap);
        }
    }

    /**
     * 为了显示和UI设计一致
     * @param bitmap
     */
    private void setBitmapAndResize(Bitmap bitmap){
        if(bitmap == null){
            mShareImage.setImageBitmap(null);
            return;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int maxWidth = mContext.getResources().getDimensionPixelSize(R.dimen.share_imageview_max_width);
        int maxHeight = mContext.getResources().getDimensionPixelSize(R.dimen.share_imageview_max_height);
        if(width * 1.0f / height > maxWidth * 1.0f / maxHeight){//比较宽  恢复默认即可
            ViewGroup.LayoutParams lp = mShareImage.getLayoutParams();
            lp.width = maxWidth;
            lp.height = maxHeight;
            mShareImage.setLayoutParams(lp);
        } else{//比较高
            ViewGroup.LayoutParams lp = mShareImage.getLayoutParams();
            lp.width = width * maxHeight / height;
            lp.height = maxHeight;
            mShareImage.setLayoutParams(lp);
        }
        mShareImage.setImageBitmap(bitmap);
    }

    /**
     * 绑定监听
     */
    private void bindListenerBottom(){
        mBottomDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {//dialog隐藏的时候将广告移除
            @Override
            public void onDismiss(DialogInterface dialog) {
            }
        });
        ItemClickSupport itemClickSupport = new ItemClickSupport(mShareRecyclerView, mRecyclerAdapter);
        mRecyclerAdapter.setItemClickSupport(itemClickSupport);
        itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View view, int position, long id) {
                IItemBean itemBean = mRecyclerAdapter.getData().get(position);
                if (itemBean instanceof ShareImageAdapter.ShareImageItemDataBean) {
                    ShareImageItem.ShareImageItemData itemData = ((ShareImageAdapter.ShareImageItemDataBean)itemBean).get();
                    String pkgName = itemData.getmPkgName();
                    String activityName = itemData.getmActivityName();
                    final boolean isImage = MediaTypeUtil.isImage(mBitmapBean.mType);
                    boolean isGif = MediaTypeUtil.isGif(mBitmapBean.mType);
                    if (pkgName == null) {//Image 才有
                        View parent = mBottomDialog.getWindow().findViewById(android.support.design.R.id.design_bottom_sheet);
                        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
                        behavior.setPeekHeight(DeviceUtils.dip2px(mContext, 375));
                        behavior.setState(STATE_COLLAPSED);


                        List<IItemBean> datas = new ArrayList<>();
                        List<ShareImageItem.ShareImageItemData> allShareTools = ShareImageTools.getAllShareTools(mContext, MediaTypeUtil.isImage(mBitmapBean.mType));
                        if (allShareTools != null) {
                            for (ShareImageItem.ShareImageItemData data : allShareTools) {
                                ShareImageAdapter.ShareImageItemDataBean shareImageItemDataBean = new ShareImageAdapter.ShareImageItemDataBean();
                                shareImageItemDataBean.set(data);
                                datas.add(shareImageItemDataBean);
                            }
                        }
                        mRecyclerAdapter.setData(datas);
                        mRecyclerAdapter.notifyDataSetChanged();
                        mShareRecyclerView.scrollBy(0, 5);
                    } else if (pkgName.equals(ShareImageTools.PRINT_LOCAL_PACKAGE_NAME)) {
                        printAtHome();
                    } else {
                        boolean isInstall = ShareImageTools.getAppIsInstalled(mContext, pkgName);
                        if (isInstall) {
                            final ShareImageItem.ShareImageItemData siid = itemData;
                            boolean result = true;
                            if (ShareImageTools.INSTAGRAM_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(siid.getmPkgName())) {//Instagram不需要保存工具信息
                                if (isImage) {
                                    result = ShareImageTools.startInstagramShareActivity(mContext, siid.getmPkgName(),
                                            siid.getmActivityName(), transferForIns(mBitmapBean.mUri), MediaTypeUtil.isImage(mBitmapBean.mType));
                                } else {
                                    result = ShareImageTools.startInstagramShareActivity(mContext, siid.getmPkgName(),
                                            siid.getmActivityName(), mBitmapBean.mUri, MediaTypeUtil.isImage(mBitmapBean.mType));
                                }
                            } else {
                                result = ShareImageTools.startShareActivity(mContext, siid.getmPkgName(),
                                        siid.getmActivityName(), mBitmapBean);
                                if (isImage) {
                                    DataManager.getInstance().recordShareImageTool(siid.getmPkgName(), siid.getmActivityName());
                                }
                            }
                            if (result) {
                                mBottomDialog.dismiss();
                                StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_SHARE_CHANNEL + "_" + siid.getmLabel());
                                SPDataManager.setShareCountOfDay(SPDataManager.getShareCountOfDay() + 1);
                                statisticsShare(siid.getmLabel());
                            } else {
                                Toast.makeText(mContext, R.string.not_install, Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(mContext, R.string.not_install, Toast.LENGTH_SHORT).show();
                        }

                        if (ShareImageTools.FACEBOOK_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.INSTAGRAM_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.WEIXIN_SEND_PIC_TO_PENGYOUQUAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.QQ_SEND_PIC_TO_KONGJIAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.SINA_SEND_PIC_TO_PENGYOUQUAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.WHATSAPP_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        }
                    }
                }
            }

        });
    }

    public void statisticsShare(String useWhat) {
        //通知流程
        NotifyStoreUpdateStatisticUtil.statisticShare(useWhat);
        //主页按钮流程
        if(!TextUtils.isEmpty(mMainButtonEntrance)) {
            StatisticsUtils.statisticsMainBtnPreviewShare(mMainButtonEntrance, useWhat);
        }
    }

    /**
     * 绑定监听
     */
    private void bindListener(){
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {//dialog隐藏的时候将广告移除
            @Override
            public void onDismiss(DialogInterface dialog) {
            }
        });
        mShareGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (view instanceof ShareImageLittleItem) {
                    ShareImageLittleItem flag = (ShareImageLittleItem) view;
                    String pkgName = flag.getItemData().getmPkgName();
                    String activityName = flag.getItemData().getmActivityName();
                    final boolean isImage = MediaTypeUtil.isImage(mBitmapBean.mType);
                    boolean isGif = MediaTypeUtil.isGif(mBitmapBean.mType);
                    if (pkgName == null) {//Image 才有
                        mShareImageAdapter.setData(ShareImageTools.getAllShareTools(mContext, MediaTypeUtil.isImage(mBitmapBean.mType)));
                        mShareImageAdapter.notifyDataSetChanged();
                    } else if (pkgName.equals(ShareImageTools.PRINT_LOCAL_PACKAGE_NAME)) {
                        printAtHome();
                    } else {
                        boolean isInstall = ShareImageTools.getAppIsInstalled(mContext, pkgName);
                        if (isInstall) {
                            final ShareImageItem.ShareImageItemData siid = flag.getItemData();
                            boolean result = true;
                            if (ShareImageTools.INSTAGRAM_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(siid.getmPkgName())) {//Instagram不需要保存工具信息
                                //改为 1 : 1 比例
                                if (isImage) {
                                    result = ShareImageTools.startInstagramShareActivity(mContext, siid.getmPkgName(),
                                            siid.getmActivityName(), transferForIns(mBitmapBean.mUri), MediaTypeUtil.isImage(mBitmapBean.mType));
                                } else {
                                    result = ShareImageTools.startInstagramShareActivity(mContext, siid.getmPkgName(),
                                            siid.getmActivityName(), mBitmapBean.mUri, MediaTypeUtil.isImage(mBitmapBean.mType));
                                }
                            } else {
                                result = ShareImageTools.startShareActivity(mContext, siid.getmPkgName(),
                                        siid.getmActivityName(), mBitmapBean);
                                if(isImage) {
                                    DataManager.getInstance().recordShareImageTool(siid.getmPkgName(), siid.getmActivityName());
                                }
                            }
                            if (result) {
                                mDialog.dismiss();
                                SPDataManager.setShareCountOfDay(SPDataManager.getShareCountOfDay()+1);
                            } else {
                                Toast.makeText(mContext, R.string.not_install, Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(mContext, R.string.not_install, Toast.LENGTH_SHORT).show();
                        }

                        if (ShareImageTools.FACEBOOK_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.INSTAGRAM_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.WEIXIN_SEND_PIC_TO_PENGYOUQUAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.QQ_SEND_PIC_TO_KONGJIAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.SINA_SEND_PIC_TO_PENGYOUQUAN_PACKAGE_NAME.equals(pkgName)) {
                        } else if (ShareImageTools.WHATSAPP_SEND_PIC_TO_SHARE_PACKAGE_NAME.equals(pkgName)) {
                        }
                    }
                }
            }
        });
    }

    //图片转换为 1:1
    public static Uri transferForIns(Uri uri) {
        try {
            InputStream input = ComponentContext.getContext().getContentResolver().openInputStream(uri);
            BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
            onlyBoundsOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
            input.close();
            int originWidth = onlyBoundsOptions.outWidth;
            int originHeight = onlyBoundsOptions.outHeight;
            float ratio = originWidth * 1.0f / originHeight;
            if (ratio == 1) {
                return uri;
            } else if (ratio > 1) {
                input = ComponentContext.getContext().getContentResolver().openInputStream(uri);
                onlyBoundsOptions.inJustDecodeBounds = false;
                Bitmap bitmap = BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
                input.close();
                Bitmap newBitmap = Bitmap.createBitmap(originWidth, originWidth, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(newBitmap);
                canvas.drawColor(Color.WHITE);
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setFilterBitmap(true);
                canvas.drawBitmap(bitmap, 0, (originWidth - originHeight) / 2, paint);
                bitmap.recycle();
                Uri parse = Uri.parse(MediaStore.Images.Media.insertImage(ComponentContext.getContext().getContentResolver(), newBitmap, null, null));
                SPDataManager.setInsShareImgUri(parse.toString());
                return parse;
            } else {
                input = ComponentContext.getContext().getContentResolver().openInputStream(uri);
                onlyBoundsOptions.inJustDecodeBounds = false;
                Bitmap bitmap = BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
                input.close();
                Bitmap newBitmap = Bitmap.createBitmap(originHeight, originHeight, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(newBitmap);
                canvas.drawColor(Color.WHITE);
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setFilterBitmap(true);
                canvas.drawBitmap(bitmap, (originHeight - originWidth) / 2, 0, paint);
                bitmap.recycle();
                String newUrl = MediaStore.Images.Media.insertImage(ComponentContext.getContext().getContentResolver(), newBitmap, null, null);
                Uri parse = uri;
                if (!TextUtils.isEmpty(newUrl)) {
                    parse = Uri.parse(newUrl);
                }
                SPDataManager.setInsShareImgUri(parse.toString());
                return parse;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void deleteInsImg() {
        String insShareImgUri = SPDataManager.getInsShareImgUri();
        if (!TextUtils.isEmpty(insShareImgUri)) {
            String[] split = insShareImgUri.split("#");
            for(String s : split) {
                if(!TextUtils.isEmpty(s)) {
                    Uri uri = Uri.parse(s);
                    try {
                        ImageHelper.deleteMediaByUri(ComponentContext.getContext(), uri, getPath(uri));
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            SPDataManager.setInsShareImgUri("");
        }
    }

    private static String getPath(Uri uri) {
        String[] projection = {MediaStore.Video.Media.DATA};
        Cursor cursor = ComponentContext.getContext().getContentResolver().query(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    /**
     * Print at home
     */
    private void printAtHome() {
        if (mBitmapBean == null) return;
        boolean isAble = mBitmapBean.mIsAble;
        boolean isImage = MediaTypeUtil.isImage(mBitmapBean.mType);
        if (isAble && isImage) {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                }
            }.start();
        }
    }

}
